package com.lc.app.myaccount.activity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lc.app.myaccount.R;
import com.lc.app.myaccount.dao.OutAccountDao;
import com.lc.app.myaccount.dao.UserDao;
import com.lc.app.myaccount.entity.OutAccount;
import com.lc.app.myaccount.entity.UserEntity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class OutAccountActivity extends AppCompatActivity {
    private EditText etMoney;
    private EditText etPlace;
    private EditText etType;

    private TextView initDate;

    private Button outCancel;
    private Button outSave;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_out_account);
        initDate = (TextView) findViewById(R.id.initDate);

        final Calendar cal = Calendar.getInstance();
        final SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        String now = sd.format(cal.getTime());
        initDate.setText(now);
        final  DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar c = Calendar.getInstance();
                c.set(Calendar.YEAR,year);
                c.set(Calendar.MONTH,monthOfYear);
                c.set(Calendar.DAY_OF_MONTH,dayOfMonth);
                String chooseDate = sd.format(c.getTime());
                initDate.setText(chooseDate);
            }
        },cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
        initDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show();
            }
        });

        etMoney = (EditText) findViewById(R.id.etMoney);
        etPlace = (EditText) findViewById(R.id.etPlace);
        etType = (EditText) findViewById(R.id.etType);
        outCancel = (Button) findViewById(R.id.outCancel);
        outSave = (Button) findViewById(R.id.outSave);

        final Intent intent = new Intent(OutAccountActivity.this.getApplicationContext(),Main.class);
        outCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               AlertDialog.Builder builder = new AlertDialog.Builder(OutAccountActivity.this);
                builder.setTitle("确定退出么？");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        startActivity(intent);
                    }
                });

                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

        outSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String money = etMoney.getText().toString();
                String place = etPlace.getEditableText().toString();
                String type = etPlace.getEditableText().toString();
                String date = initDate.getText().toString();
                if( money == null || "".equals(money)){
                    Toast.makeText(getApplicationContext(),"金额不能为空",Toast.LENGTH_SHORT).show();
                }else if(place == null || "".equals(place)){
                    Toast.makeText(getApplicationContext(),"地方不能为空",Toast.LENGTH_SHORT).show();
                }else if(type == null || "".equals(type)){
                    Toast.makeText(getApplicationContext(),"类型不能为空",Toast.LENGTH_SHORT).show();
                }else if(date == null || "".equals(date)){
                    Toast.makeText(getApplicationContext(),"日期不能为空",Toast.LENGTH_SHORT).show();
                }
                double moneyD = 0;
                try{
                    moneyD = Double.parseDouble(money);
                }catch(Exception e){
                    Toast.makeText(getApplicationContext(),"金额不正确",Toast.LENGTH_SHORT).show();
                }

                UserDao userDao = new UserDao(getApplicationContext());
                UserEntity user = userDao.findByName("admin");
                OutAccount out = new OutAccount();
                out.setAddress(place);
                out.setMoney(moneyD);
                out.setType(type);
                out.setTime(date);
                out.setUserId(user.get_id());
                out.setMark("");

                OutAccountDao outDao = new OutAccountDao(getApplicationContext());
                outDao.add(out);
                Toast.makeText(getApplicationContext(), "数据保存成功", Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }
        });
    }



}
